package com.lenovo.manufacture.Fragment;


import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.lenovo.manufacture.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;

/**
 * A simple {@link Fragment} subclass.
 */
public class BlankFragment_eight extends Fragment {


    @BindView(R.id.lv_one)
    ListView lvOne;
    @BindView(R.id.tv_kc)
    TextView tvKc;
    @BindView(R.id.tv_zje)
    TextView tvZje;
    private List<Seven_list> seven_lists = new ArrayList<>();
    private int j;
    public BlankFragment_eight() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_eight, container, false);
        ButterKnife.bind(this, view);
        initview1();
        return view;
    }

    public interface BlogSerivea {
        @GET("getchargelog")
        Call<ResponseBody> Bloga();
    }

    private void initview1() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/")
                .build();
        BlogSerivea blogSerive = retrofit.create(BlogSerivea.class);
        Call<ResponseBody> call = blogSerive.Bloga();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Gson gson = new Gson();
                String s = null;
                try {
                    s = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Seven_list carlist = gson.fromJson(s, Seven_list.class);
                seven_lists.add(carlist);
                for (int i=0;i<seven_lists.get(0).getData().size();i++){
                    j= (int) (j+seven_lists.get(0).getData().get(i).getAmount());
                }
                tvKc.setText("库存信息:"+"共有"+seven_lists.get(0).getData().size()+"条数据");
                tvZje.setText("总余额:"+j+"元");
                lvOne.setAdapter(new meAdapter());
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });
    }

    class meAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return seven_lists.get(0).getData().size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View view1 = View.inflate(getContext(), R.layout.fragmeng_eight_one, null);
            ViewHolder viewHolder = new ViewHolder(view1);
            viewHolder.tvCzOne.setText(seven_lists.get(0).getData().get(i).getWho() + "");
            viewHolder.tvCzjeOne.setText(seven_lists.get(0).getData().get(i).getCharge() + "");
            viewHolder.tvCarnoOne.setText(seven_lists.get(0).getData().get(i).getCarno() + "");
            viewHolder.tvYeOne.setText(seven_lists.get(0).getData().get(i).getAmount() + "");
            viewHolder.tvCztimeOne.setText(seven_lists.get(0).getData().get(i).getTime() + "");
            return view1;
        }

        class ViewHolder {
            @BindView(R.id.tv_cz_one)
            TextView tvCzOne;
            @BindView(R.id.tv_czje_one)
            TextView tvCzjeOne;
            @BindView(R.id.tv_carno_one)
            TextView tvCarnoOne;
            @BindView(R.id.tv_ye_one)
            TextView tvYeOne;
            @BindView(R.id.tv_cztime_one)
            TextView tvCztimeOne;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
